cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 17)
project (mock)

add_subdirectory(thirdparty/knet/)

message(STATUS "make project dir: ${PROJECT_SOURCE_DIR}")

INCLUDE_DIRECTORIES(
    ${PROJECT_SOURCE_DIR}/../../thirdparty/include
    ${PROJECT_SOURCE_DIR}/thirdparty/knet/knet/
    ${PROJECT_SOURCE_DIR}/thirdparty/cxxopts/
    ${PROJECT_SOURCE_DIR}/../../src/include/root/
    ${PROJECT_SOURCE_DIR}/../../src/include/example/
    ${PROJECT_SOURCE_DIR}/../../src/include/example/Service/proxy/
    ${PROJECT_SOURCE_DIR}/../../src/include/example/ServiceDynamic/proxy/
    ${PROJECT_SOURCE_DIR}/../../src/include/example/Service/stub/
    ${PROJECT_SOURCE_DIR}/../../src/include/example/ServiceDynamic/stub/
)

set(SRC_LIST
    ${PROJECT_SOURCE_DIR}/src/main.cpp
    ${PROJECT_SOURCE_DIR}/src/mock_transport.cpp
    ${PROJECT_SOURCE_DIR}/src/mock_caller.cpp
)

set(PROXY_SRC_LIST
    ${PROJECT_SOURCE_DIR}/src/proxy.cpp
    ${PROJECT_SOURCE_DIR}/src/mock_transport.cpp
    ${PROJECT_SOURCE_DIR}/src/mock_caller.cpp
)

set(PROXY_CO_SRC_LIST
    ${PROJECT_SOURCE_DIR}/src/proxy.cpp
    ${PROJECT_SOURCE_DIR}/src/mock_transport.cpp
    ${PROJECT_SOURCE_DIR}/src/mock_caller_co.cpp
)

set(SERVER_SRC_LIST
    ${PROJECT_SOURCE_DIR}/src/server.cpp
    ${PROJECT_SOURCE_DIR}/src/mock_transport.cpp
)

set(SERVER_CO_SRC_LIST
    ${PROJECT_SOURCE_DIR}/src/server_co.cpp
    ${PROJECT_SOURCE_DIR}/src/mock_transport.cpp
)

if (MSVC)
    foreach(var
        CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
        CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
        )
        if(${var} MATCHES "/MD")
            string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}")
        endif()
    endforeach()
else()
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "CLANG")
        SET(CMAKE_CXX_FLAGS "-m64 -g -O2 -std=c++17 -Wall")
        set(CMAKE_CXX_COMPILER "clang++")
    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        SET(CMAKE_CXX_FLAGS "-m64 -g -O2 -std=c++17 -Wall")
        set(CMAKE_CXX_COMPILER "g++")
    endif()
endif()

add_executable(mock ${SRC_LIST})
add_executable(proxy ${PROXY_SRC_LIST})
add_executable(proxy_co ${PROXY_CO_SRC_LIST})
add_executable(server ${SERVER_SRC_LIST})
add_executable(server_co ${SERVER_CO_SRC_LIST})

target_compile_definitions(mock PUBLIC
$<$<CONFIG:Debug>: -DDEBUG>
$<$<CONFIG:Release>: -DRELEASE>
)

target_compile_definitions(proxy PUBLIC
$<$<CONFIG:Debug>: -DDEBUG>
$<$<CONFIG:Release>: -DRELEASE>
)

target_compile_definitions(proxy_co PUBLIC
$<$<CONFIG:Debug>: -DDEBUG>
$<$<CONFIG:Release>: -DRELEASE>
)

target_compile_definitions(server PUBLIC
$<$<CONFIG:Debug>: -DDEBUG>
$<$<CONFIG:Release>: -DRELEASE>
)

target_compile_definitions(server_co PUBLIC
$<$<CONFIG:Debug>: -DDEBUG>
$<$<CONFIG:Release>: -DRELEASE>
)

if (MSVC)
    set_target_properties(mock PROPERTIES COMPILE_FLAGS "/EHsc /W3")
    set(service_libs
        debug ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/Debug/libknet.a optimized ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/Release/libknet.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/root/Debug/librpc.a optimized ${PROJECT_SOURCE_DIR}/../../lib/root/Release/librpc.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Debug/libService_proxy.a optimized ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Release/libService_proxy.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Debug/libServiceDynamic_proxy.a optimized ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Release/libServiceDynamic_proxy.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/example/Debug/libexample.a optimized ${PROJECT_SOURCE_DIR}/../../lib/example/Release/libexample.a
        debug ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/windows/debug/libprotobufd.lib optimized ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/windows/release/libprotobuf.lib
    )
    SET_TARGET_PROPERTIES(mock PROPERTIES 
        LINK_FLAGS_DEBUG "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Debug/libService.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Debug/libService_stub.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/Debug/libServiceDynamic_stub.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Debug/libService_proxy.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Debug/libServiceDynamic_proxy.a"
        LINK_FLAGS_RELEASE "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Release/libService.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Release/libService_stub.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/Release/libServiceDynamic_stub.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Release/libService_proxy.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Release/libServiceDynamic_proxy.a"
        RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
        RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    )

    target_link_libraries(mock ${service_libs})

    set_target_properties(proxy PROPERTIES COMPILE_FLAGS "/EHsc /W3")
    set(proxy_libs
        debug ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/Debug/libknet.a optimized ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/Release/libknet.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/root/Debug/librpc.a optimized ${PROJECT_SOURCE_DIR}/../../lib/root/Release/librpc.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Debug/libService_proxy.a optimized ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Release/libService_proxy.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Debug/libServiceDynamic_proxy.a optimized ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Release/libServiceDynamic_proxy.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/example/Debug/libexample.a optimized ${PROJECT_SOURCE_DIR}/../../lib/example/Release/libexample.a
        debug ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/windows/debug/libprotobufd.lib optimized ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/windows/release/libprotobuf.lib
    )
    SET_TARGET_PROPERTIES(proxy PROPERTIES 
        LINK_FLAGS_DEBUG "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Debug/libService_proxy.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Debug/libServiceDynamic_proxy.a"
        LINK_FLAGS_RELEASE "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Release/libService_proxy.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Release/libServiceDynamic_proxy.a"
        RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
        RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
        )

    target_link_libraries(proxy ${proxy_libs})
    
    set_target_properties(proxy_co PROPERTIES COMPILE_FLAGS "/EHsc /W3")
    set(proxy_libs
        debug ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/Debug/libknet.a optimized ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/Release/libknet.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/root/Debug/librpc.a optimized ${PROJECT_SOURCE_DIR}/../../lib/root/Release/librpc.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Debug/libService_proxy.a optimized ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Release/libService_proxy.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Debug/libServiceDynamic_proxy.a optimized ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Release/libServiceDynamic_proxy.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/example/Debug/libexample.a optimized ${PROJECT_SOURCE_DIR}/../../lib/example/Release/libexample.a
        debug ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/windows/debug/libprotobufd.lib optimized ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/windows/release/libprotobuf.lib
    )

    SET_TARGET_PROPERTIES(proxy_co 
    PROPERTIES 
    LINK_FLAGS_DEBUG "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Debug/libService_proxy.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Debug/libServiceDynamic_proxy.a"
    LINK_FLAGS_RELEASE "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/Release/libService_proxy.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/Release/libServiceDynamic_proxy.a"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    )

    target_link_libraries(proxy_co ${proxy_libs})

    set_target_properties(server PROPERTIES COMPILE_FLAGS "/EHsc /W3")
    set(server_libs
        debug ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/Debug/libknet.a optimized ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/Release/libknet.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/root/Debug/librpc.a optimized ${PROJECT_SOURCE_DIR}/../../lib/root/Release/librpc.a
        debug ${PROJECT_SOURCE_DIR}/../../lib/example/Debug/libexample.a optimized ${PROJECT_SOURCE_DIR}/../../lib/example/Release/libexample.a
        debug ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/windows/debug/libprotobufd.lib optimized ${PROJECT_SOURCE_DIR}/../../thirdparty/lib/windows/release/libprotobuf.lib
    )
    SET_TARGET_PROPERTIES(server 
    PROPERTIES 
    LINK_FLAGS_DEBUG "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Debug/libService.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Debug/libService_stub.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/Debug/libServiceDynamic_stub.a"
    LINK_FLAGS_RELEASE "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Release/libService.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Release/libService_stub.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/Release/libServiceDynamic_stub.a"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    )
    
    target_link_libraries(server ${server_libs})

    set_target_properties(server_co PROPERTIES COMPILE_FLAGS "/EHsc /W3")
    SET_TARGET_PROPERTIES(server_co 
    PROPERTIES 
    LINK_FLAGS_DEBUG "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Debug/libService.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Debug/libService_stub.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/Debug/libServiceDynamic_stub.a"
    LINK_FLAGS_RELEASE "/WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Release/libService.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/Release/libService_stub.a /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/Release/libServiceDynamic_stub.a"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    )
    target_link_libraries(server_co ${server_libs})

else()

    LINK_DIRECTORIES(
        ${PROJECT_SOURCE_DIR}/../../thirdparty/lib
    )

    target_link_libraries(mock
        ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/libknet.a
        ${PROJECT_SOURCE_DIR}/../../lib/root/librpc.a
        ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/libService_proxy.a
        ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/libServiceDynamic_proxy.a
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/libService.a -Wl,--no-whole-archive
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/libService_stub.a -Wl,--no-whole-archive
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/libServiceDynamic_stub.a -Wl,--no-whole-archive
        ${PROJECT_SOURCE_DIR}/../../lib/example/libexample.a
        libprotobuf.a
        -lpthread
        -ldl
    )

    target_link_libraries(mock
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    ) 

    target_link_libraries(proxy
        ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/libknet.a
        ${PROJECT_SOURCE_DIR}/../../lib/root/librpc.a
        ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/libService_proxy.a
        ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/libServiceDynamic_proxy.a
        ${PROJECT_SOURCE_DIR}/../../lib/example/libexample.a
        libprotobuf.a
        -lpthread
        -ldl
    )

    target_link_libraries(proxy
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    )
    
    target_link_libraries(proxy_co
        ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/libknet.a
        ${PROJECT_SOURCE_DIR}/../../lib/root/librpc.a
        ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/Service/libService_proxy.a
        ${PROJECT_SOURCE_DIR}/../../lib/proxy/example/ServiceDynamic/libServiceDynamic_proxy.a
        ${PROJECT_SOURCE_DIR}/../../lib/example/libexample.a
        libprotobuf.a
        -lpthread
        -ldl
    )

    target_link_libraries(proxy_co
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    )

    target_link_libraries(server
        ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/libknet.a
        ${PROJECT_SOURCE_DIR}/../../lib/root/librpc.a
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/libService.a -Wl,--no-whole-archive
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/libService_stub.a -Wl,--no-whole-archive
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/libServiceDynamic_stub.a -Wl,--no-whole-archive
        ${PROJECT_SOURCE_DIR}/../../lib/example/libexample.a
        libprotobuf.a
        -lpthread
        -ldl
    )

    target_link_libraries(server
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    )

    target_link_libraries(server_co
        ${PROJECT_SOURCE_DIR}/thirdparty/knet/lib/libknet.a
        ${PROJECT_SOURCE_DIR}/../../lib/root/librpc.a
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/libService.a -Wl,--no-whole-archive
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/Service/libService_stub.a -Wl,--no-whole-archive
        -Wl,--whole-archive ${PROJECT_SOURCE_DIR}/../../lib/stub/example/ServiceDynamic/libServiceDynamic_stub.a -Wl,--no-whole-archive
        ${PROJECT_SOURCE_DIR}/../../lib/example/libexample.a
        libprotobuf.a
        -lpthread
        -ldl
    )

    target_link_libraries(server_co
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_SOURCE_DIR}/../debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_SOURCE_DIR}/../release"
    )
endif()

